﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;

namespace Jy123.Web.Images
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class ImgCode : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["at"] == null)
            {
                CreateImages(CookieCheckCode(context), context);
            }
            else
            {
                CreateImages(CookieCheckCode(context.Request.QueryString["at"], context), context);
            }
        }

        /// <summary>
        /// 生成验证图片
        /// </summary>
        /// <param name="checkCode">验证字符</param>
        /// <param name="context"></param>
        protected void CreateImages(string checkCode, HttpContext context)
        {
            int iwidth = (int)(checkCode.Length * 15);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 30);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.WhiteSmoke);
            //定义颜色
            List<Color> c = new List<Color>(){ 
                Color.Black, 
                Color.Red, 
                Color.DarkBlue, 
                Color.Green, 
                Color.Orange,
                Color.Brown, 
                Color.DarkCyan, 
                Color.Purple, 
                Color.SkyBlue };
            //定义字体 
            List<string> font = new List<string>(){ 
                "Verdana", 
                "Microsoft Sans Serif", 
                "Comic Sans MS", 
                "Arial", 
                "宋体", 
                "Comic Sans MS" };
            Random rand = new Random();
            //随机输出噪点
            for (int i = 0; i < 150; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawPie(new Pen(Color.LightGray, 0), x, y, 6, 6, 1, 1);
            }

            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(7);
                int cyindex = rand.Next(7);
                int findex = rand.Next(6);
                Font _font = new System.Drawing.Font(font[findex], 15, System.Drawing.FontStyle.Bold);
                Brush b = new System.Drawing.SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), _font, b, 3 + (i * 12), ii);
            }
            g.DrawRectangle(new Pen(Color.Wheat), 0, 0, image.Width - 1, image.Height - 1);
            //输出到浏览器

            try
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                context.Response.ClearContent();
                context.Response.ContentType = "Image/Jpeg";
                context.Response.BinaryWrite(ms.ToArray());
                
            }
            catch { }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
        private static string CookieCheckCode(HttpContext context)
        {
            int number;
            char code;
            string checkCode = string.Empty;

            System.Random random = new Random();

            for (int i = 0; i < 5; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));

                checkCode += code.ToString();
            }
            Common.Cookie.SetCookieM("checkcode", checkCode, 1);
            return checkCode;
        }
        private static string CookieCheckCode(string cookieName, HttpContext context)
        {
            int number;
            char code;
            string checkCode = string.Empty;

            System.Random random = new Random();

            for (int i = 0; i < 5; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));

                checkCode += code.ToString();
            }
            Common.Cookie.SetCookieM(cookieName, checkCode, 1);
            return checkCode;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
